package com.geoboard.tools;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.nfc.NfcAdapter.CreateBeamUrisCallback;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.Projection;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.GroundOverlayOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;

public class BoardOverlayGenerator{
	
	private static final int DEFAULT_HEIGHT = 500;
	private static final int DEFAULT_WIDTH = 500;
	private static final int DEFAULT_FONT_SIZE = 50;
	private static final float DEFAULT_FONT_SCALE = 1.8f;
	
	private static final Paint fontPaint = new Paint();
	
	private static final int fontColor = 0xffffffff;
	
	private static Projection proj = null;
	
	public static GroundOverlayOptions genOverlay(String boardName, 
			LatLng northEast, LatLng southWest, int bkgdColor, GoogleMap map){
		
		double width = DEFAULT_WIDTH;
		double height = DEFAULT_HEIGHT;
		
		if(proj == null){
			proj = map.getProjection();
		}
		
		Point ne = proj.toScreenLocation(northEast);
		Point sw = proj.toScreenLocation(southWest);
		
		double dx = ne.x - sw.x;
		double dy = sw.y - ne.y;
		
		if(dx >	dy){	// area is more wide than tall
			height *= dy/dx;			
		} else {		// area is more tall than wide
			width *= dx/dy;
		}
		
		
		
		Bitmap bMap = Bitmap.createBitmap((int)width, (int)height, Bitmap.Config.ARGB_8888);
		
		Canvas c = new Canvas(bMap);
		
		c.drawColor(bkgdColor);
		
		int fontSize = DEFAULT_FONT_SIZE;
		
		if(fontSize * boardName.length() > DEFAULT_FONT_SCALE * width){
			fontSize = (int) (DEFAULT_FONT_SCALE*width / boardName.length());			
		}
		
		fontPaint.setAntiAlias(true);
		fontPaint.setColor(fontColor);
		fontPaint.setTextSize(fontSize);
		
		c.drawText(boardName, 0.25f * fontSize, 1.25f * fontSize, fontPaint);
		
		GroundOverlayOptions goo = new GroundOverlayOptions();
		goo.image(BitmapDescriptorFactory.fromBitmap(bMap));
		LatLngBounds llb = new LatLngBounds(southWest, northEast);	
		goo.positionFromBounds(llb);
		
		goo.zIndex(100);
		
		return goo;
	}

}
